
const express = require('express');
const cors = require('cors');
const dotenv = require('dotenv');
const sequelize = require('./config/database');

// 导入路由
const adminRoutes = require('./routers/adminLoginRoutes');
const studentRoutes = require('./routers/studentRegisterRoutes');
const studentLoginRoutes = require('./routers/studentLoginRoutes');
const sysAdminRoutes = require('./routers/sysAdminLoginRoutes');
const getStudentInfoRoutes = require('./routers/getStudentInfoRoutes');
const deleteStudent = require('./routers/delete_student_routes');
const createClubs = require('./routers/clubsRouter/createRouter');
const allClubs=require('./routers/clubsRouter/showRouter');
const deleteClub = require('./routers/clubsRouter/deleteRouter'); // 新增导入
const updateClub = require('./routers/clubsRouter/putRouter'); // 新增导入
const joinClubRouter = require('./routers/studentClubRouter/SjoinClub');
const deleteSClubRouter = require('./routers/studentClubRouter/delectSClub');
const putSclubRouter = require('./routers/studentClubRouter/putSclub');
const showSClubRouter = require('./routers/studentClubRouter/showSClub');
const createActivities = require('./routers/activitiesRouter/createActivities');
const putActivities = require('./routers/activitiesRouter/putActivities');
const showActivities = require('./routers/activitiesRouter/showActivities');
const StudentjoinActivity = require('./routers/studentActivityRouter/StudentjoinActivity');
const cleanJoin = require('./routers/studentActivityRouter/cleanJoin');
const showSactivity = require('./routers/studentActivityRouter/showSactivity');
const StudentAttendActivity = require('./routers/studentActivityRouter/StudentAttendActivity');
const analsyTable = require('./routers/analsy/analsyTable');
const addPrize = require('./routers/Prizes/Prizes');
const uploadRouter = require('./routers/uploadImage');
const exchangePrize = require('./routers/Prizes/ExchangePrize');
dotenv.config();

const app = express();
app.use(cors());
app.use(express.json());


// 测试数据库连接
sequelize.authenticate()
  .then(() => {
    console.log('✅ 数据库连接成功');
    return sequelize.sync();// 只确保表存在，不修改结构
  })
  .then(() => {
    console.log('✅ 数据库表已同步');
  })
  .catch(err => {
    console.error('❌ 数据库连接失败:', err);
  });

// 根路由
app.get('/', (req, res) => {
  res.json({ message: '欢迎使用校园积分系统 API' });
});

// 使用路由
app.use('/api', adminRoutes);
app.use('/api', studentRoutes);
app.use('/api', studentLoginRoutes);
app.use('/api', sysAdminRoutes);
app.use('/api',getStudentInfoRoutes)
app.use('/api',deleteStudent)
app.use('/api',createClubs)
app.use('/api',allClubs)
app.use('/api',deleteClub) // 新增路由
app.use('/api',updateClub) // 新增路由
app.use('/api', joinClubRouter);
app.use('/api', deleteSClubRouter);
app.use('/api', putSclubRouter);
app.use('/api', showSClubRouter);
app.use('/api', createActivities);
app.use('/api', putActivities);
app.use('/api', showActivities);
app.use('/api', StudentjoinActivity);
app.use('/api', cleanJoin);
app.use('/api', showSactivity);
app.use('/api', StudentAttendActivity);
app.use('/api', analsyTable);
app.use('/api', addPrize);
app.use('/api', exchangePrize);

//图片上传路由
app.use('/uploads', express.static('uploads'));
app.use(uploadRouter)

// 启动服务器
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
  console.log(`🚀 服务器运行在端口 ${PORT}`);
});
